//
//  MyBalanceController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/6.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class MyBalanceController: BaseViewController {
    
    var page = -999
    
    var type = "0"

    var dataModel : MyGoldBeanModel?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MyGoldBeanTopCell.self), bundle: nil), forCellReuseIdentifier: MyGoldBeanTopCell.identifer())

       plainTableView.register(UINib(nibName: StringFromClass(classType: MyBalanceListCell.self), bundle: nil), forCellReuseIdentifier: MyBalanceListCell.identifer())
        
       plainTableView.register(UINib(nibName: StringFromClass(classType: MyBalanceSecendCell.self), bundle: nil), forCellReuseIdentifier: MyBalanceSecendCell.identifer())
        
       plainTableView.register(UINib(nibName: StringFromClass(classType: MyBalanceHeaderView.self), bundle: nil), forHeaderFooterViewReuseIdentifier: MyBalanceHeaderView.identifer())
        
       plainTableView.register(UINib(nibName: StringFromClass(classType: MyBalanceHeaderTwo.self), bundle: nil), forHeaderFooterViewReuseIdentifier: MyBalanceHeaderTwo.identifer())
        
        
        
        
        plainTableView.tableFooterView = UIView()
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()
        
        plainTableView.mj_footer = ZMRefreshFooter.init{
            
            weakSelf!.loadMoreData()
            
        }


        // Do any additional setup after loading the view.
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        SVProgressHUD.dismiss()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


}




// MARK:- 数据加载
extension MyBalanceController{
    
    func loadData() {
        page = 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        parmeter["type"] = type as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_fund&a=predepositlog", parameters: parmeter) { [weak self] (responseObject, error) in
            
            if error == nil {
                
                self!.dataModel = MyGoldBeanModel(dic: responseObject!)
                
                self!.plainTableView.mj_footer.resetNoMoreData()
                
                if self!.dataModel?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }
                
                self?.dataModel?.datas?.predeposit_info?.card_info?.available_predeposit = self?.dataModel?.datas?.predeposit_info?.available_predeposit

                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
        }
        
        
    }
    
    
    
    func loadMoreData(){
        
        page += 1
        
        var parmeter  = [String : AnyObject]()
        
        parmeter["curpage"] = "\(page)" as AnyObject
        parmeter["type"] = type as AnyObject
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_fund&a=predepositlog", parameters: parmeter) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                let temp = MyGoldBeanModel(dic: responseObject!)
                
                
                self!.dataModel?.hasmore = temp.hasmore
                
                for item in temp.datas!.list{
                    
                    
                    self?.dataModel?.datas?.list.append(item)
                    
                    
                }
                
                
                
                if self!.dataModel?.hasmore == "0" {
                    
                    self!.plainTableView.mj_footer.endRefreshingWithNoMoreData()
                    
                    
                }else{
                    
                    self!.plainTableView.mj_footer.endRefreshing()
                    
                }
                
                
                
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_footer.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
        
        
        
    }
    
    
    
    
}



// MARK:- tableview代理
extension MyBalanceController{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        return 2
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        
        if section == 0 {
            return 2
        } else {
            return dataModel?.datas?.list.count ?? 0
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        switch indexPath.section {
        case 0:
            
            if indexPath.row == 0 {
                let tempCell : MyGoldBeanTopCell =  tableView.dequeueReusableCell(withIdentifier: MyGoldBeanTopCell.identifer()) as! MyGoldBeanTopCell
                tempCell.titleStr = "可用余额"
                
                let tempStr = dataModel?.datas?.predeposit_info?.available_predeposit
                
                if tempStr == nil {
                    
                    tempCell.rightStr = "0元"
                    
                } else {
                    tempCell.rightStr = tempStr! + "元"
                    
                }
                
                tableCell = tempCell
                
            } else {
                
                let tempCell : MyBalanceSecendCell =  tableView.dequeueReusableCell(withIdentifier: MyBalanceSecendCell.identifer()) as! MyBalanceSecendCell
                
                tempCell.delegate = self
                
                tableCell = tempCell
                
            }
        default:
            
            let tempCell : MyBalanceListCell =  tableView.dequeueReusableCell(withIdentifier: MyBalanceListCell.identifer()) as! MyBalanceListCell
            
            tempCell.model = dataModel?.datas?.list[indexPath.row]
            
            tableCell = tempCell
            
            
        }
        
        
        return tableCell!
        
    }
    
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 0 {
            
            if indexPath.row == 0 {
                return 44
            } else {
                return 60
            }
            
        } else {
            
            return 64
            
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0.00001
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            
            return 0.00001
            
        } else {
            
            return 36
            
        }
    }
    
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section == 0 {
            return nil
        } else {
            
            let hehe = dataModel?.datas?.list.count ?? 0
            
            if type == "0" && hehe < 1{
                
                let temp = tableView.dequeueReusableHeaderFooterView(withIdentifier: MyBalanceHeaderTwo.identifer()) as! MyBalanceHeaderTwo
                
                return temp

            } else {
                
                let temp = tableView.dequeueReusableHeaderFooterView(withIdentifier: MyBalanceHeaderView.identifer()) as! MyBalanceHeaderView
                
                return temp

            }
            
          
        }
        
    }

    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
    }
    
    
}




extension MyBalanceController : MyBalanceSecendCellDelegate{

    func rightGetMoneyDidTap() {
        
        
        let pushV = GetMoneyController()
        
        if dataModel?.datas?.predeposit_info?.have_card == "0" {
            
            pushV.model = nil
            
            pushV.lastModel = dataModel
            
        }else{
            
            pushV.model = dataModel?.datas?.predeposit_info?.card_info
//            pushV.model = nil

            pushV.block = {[weak self] () -> () in
                
                SVProgressHUD.showSuccess(withStatus: "恭喜您提现成功!\n系统将在1-7个工作日内将退款返还到您的账户")
                
                self?.type = "0"
                
                self?.loadData()
                
            }
            
        }
        
        navigationController?.pushViewController(pushV, animated: true)
 
    }




}


